# -*-coding:utf-8-*-
# _author_ = "janehost"
import os
import smtplib
import sys
import time
import unittest
from email.mime.text import MIMEText

from mztestpro.package.HTMLTestRunner import HTMLTestRunner

reload(sys)
sys.setdefaultencoding('utf8')
# =========================邮件接收者============================
mailto_list = ["*****@qq.com"]
#============= 设置服务器，用户名、口令以及邮箱的后缀===============
mail_host = "smtp.126.com"
mail_user = "******@126.com"
mail_pass = "***********"
#===========================发送邮件============================
def send_mail(to_list, file_new):
    """
    to_list: 发给谁
    sub: 主题
    content:内容
    send_mail("hcjk08@126.com","商城测试报告"，"主要功能回归测试")
    """
    f = open(file_new, 'rb')
    mail_body = f.read()
    f.close()
    me = mail_user
    msg = MIMEText(mail_body, 'html', 'utf-8')
    msg['Subject'] = u'自动化测试报告'
    msg['From'] = me
    msg['To'] = ";".join(to_list)
    try:
        s = smtplib.SMTP()
        s.connect(mail_host, 25)
        s.login(mail_user, mail_pass)
        s.sendmail(me, to_list, msg.as_string())
        s.close()
        return True
    except Exception, e:
        print str(e)
        return False
# ==============查找测试报告目录，找到最新生成的测试报告文件==========
def new_report(testreport):
    lists = os.listdir(testreport)
    lists.sort(key=lambda fn:os.path.getatime(testreport + "\\" + fn))
    file_new = os.path.join(testreport, lists[-1])
    print (file_new)
    return file_new


if __name__ == '__main__':
    now = time.strftime("Y-%m-%d %H_%M_%S")
    filename = './bbs/report/' + now + 'result.html'
    fp = open(filename, 'wb')
    runner = HTMLTestRunner(stream=fp, title=u'商城自动化测试报告',
                            description=u'环境：windows 10 浏览器：firefox')
    discover = unittest.defaultTestLoader.discover('./bbs/test_case',
                                                   pattern='*_sta.py')
    runner.run(discover)
    fp.close()
    file_path = new_report('./bbs/report/')
    if send_mail(mailto_list, file_path):
        print u"发送成功"
    else:
        print u"发送失败"
